package org.jeecg.modules.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.hc.entity.HcCompanyFoodDisplay;
import org.jeecg.modules.hc.entity.HcCompanyFoodList;
import org.jeecg.modules.hc.entity.HcCompanyFoodSpecifications;
import org.jeecg.modules.hc.mapper.HcCompanyFoodDisplayMapper;
import org.jeecg.modules.hc.mapper.HcCompanyFoodListMapper;
import org.jeecg.modules.hc.mapper.HcCompanyFoodSpecificationsMapper;
import org.jeecg.modules.hc.service.IHcCompanyFoodListService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 企业订餐菜品设置
 * @Author: jeecg-boot
 * @Date:   2023-07-10
 * @Version: V1.0
 */
@Service
@AllArgsConstructor
public class HcCompanyFoodListServiceImpl extends ServiceImpl<HcCompanyFoodListMapper, HcCompanyFoodList> implements IHcCompanyFoodListService {

    private HcCompanyFoodListMapper foodListMapper;
    private HcCompanyFoodSpecificationsMapper specificationsMapper;
    private HcCompanyFoodDisplayMapper hcCompanyFoodDisplayMapper;

    @Override
    @Transactional
    public void saveNew(HcCompanyFoodList hcCompanyFoodList) {
        //增加商品
        String foodId = UuidUtils.getUUID();
        hcCompanyFoodList.setId(foodId);
        hcCompanyFoodList.setBuyedNum(hcCompanyFoodList.getLimitNum());
        foodListMapper.insert(hcCompanyFoodList);
        //设置规格信息
        String isUseSpecifications = hcCompanyFoodList.getIsUseSpecifications();
        if (isUseSpecifications.equals("yes")){
            List<HcCompanyFoodSpecifications> specifications = hcCompanyFoodList.getSpecifications();
            if (CollectionUtils.isEmpty(specifications)){
                throw new JeecgBootException("启用规格但是未添加规格信息");
            }
            this.addNextSpecification(specifications,foodId);
        }
    }

    @Override
    @Transactional
    public void updateNew(HcCompanyFoodList hcCompanyFoodList) {
        HcCompanyFoodList origin = foodListMapper.selectById(hcCompanyFoodList.getId());
        if (hcCompanyFoodList.getLimitNum() != origin.getLimitNum()){
            hcCompanyFoodList.setBuyedNum(hcCompanyFoodList.getLimitNum());
        }
        foodListMapper.updateById(hcCompanyFoodList);
        LambdaQueryWrapper<HcCompanyFoodSpecifications> specificationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        specificationsLambdaQueryWrapper.eq(HcCompanyFoodSpecifications::getFoodsId,hcCompanyFoodList.getId());
        specificationsMapper.delete(specificationsLambdaQueryWrapper);
        //判断是否启用规格
        String isUseSpecifications = hcCompanyFoodList.getIsUseSpecifications();
        if (isUseSpecifications.equals("yes")){
            List<HcCompanyFoodSpecifications> specifications = hcCompanyFoodList.getSpecifications();
            if (CollectionUtils.isEmpty(specifications)){
                throw new JeecgBootException("启用规格但是未添加规格信息");
            }
            this.addNextSpecification(specifications,hcCompanyFoodList.getId());
        }
    }

    @Override
    @Transactional
    public void removeNew(String id) {
        //删除展示信息
        hcCompanyFoodDisplayMapper.delete(new LambdaQueryWrapper<HcCompanyFoodDisplay>().eq(HcCompanyFoodDisplay::getFoodsListId,id));
        //删除商品信息
        LambdaQueryWrapper<HcCompanyFoodList> foodListLambdaQueryWrapper = new LambdaQueryWrapper<>();
        foodListLambdaQueryWrapper.eq(HcCompanyFoodList::getId,id);
        foodListMapper.delete(foodListLambdaQueryWrapper);
        //删除该商品的规格信息
        LambdaQueryWrapper<HcCompanyFoodSpecifications> specificationsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        specificationsLambdaQueryWrapper.eq(HcCompanyFoodSpecifications::getFoodsId,id);
        specificationsMapper.delete(specificationsLambdaQueryWrapper);
    }

    @Override
    public Integer updateBuyedNum(String id,Integer num) {
        return  foodListMapper.updateBuyedNum(id,num);
    }

    @Override
    public void resetBuyedNum() {
        foodListMapper.resetBuyedNum();
    }

    private void addNextSpecification(List<HcCompanyFoodSpecifications> specifications,String foodId){
        specifications.forEach( s -> {
            List<HcCompanyFoodSpecifications> nextSpecifications = s.getNextSpecifications();
            //判断是否启用规格
            if (CollectionUtils.isEmpty(nextSpecifications)){
                s.setFoodsId(foodId);
                String id = UuidUtils.getUUID();
                s.setId(id);
                specificationsMapper.insert(s);
            }else {
                String id = UuidUtils.getUUID();
                s.setId(id);
                s.setFoodsId(foodId);
                specificationsMapper.insert(s);
                nextSpecifications.forEach(n -> {
                    n.setParentId(id);
                });
                addNextSpecification(nextSpecifications,foodId);
            }
        });
    }

}
